<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/7/4
 * Time: 11:02
 */

namespace app\admin\controller;

use think\Db;
use think\Request;

class Submch extends Base
{
    /**
     * 子商户号列表
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $start_time = input('start_time');
            $end_time = input('end_time');
            $where['shou_is_pay'] = 2;

            if(!$start_time && !$end_time){
                $todayStart = date('Y-m-d 00:00:01');
                $where['shou_pay_time'] = ['>', $todayStart];
            }elseif ($start_time && !$end_time){
                $where['shou_pay_time'] = ['>', $start_time];
            }elseif (!$start_time && $end_time){
                $where['shou_pay_time'] = ['<', $end_time];
            }else{
                $where['shou_pay_time'] = ['BETWEEN', [$start_time, $end_time]];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 100;
            $count = Db::table('up_mchid')->count();// 获取总条数
            $lists = Db::table('up_mchid')
                ->field('mch_id, mch_code, mch_default, mch_time, mch_status')
                ->order('mch_time desc')
                ->page($Nowpage, $limits)
                ->select();
            if($lists){
                $mx = Db::table('up_shou')->where($where)->field('shou_price, shou_mchid')->select();
                foreach ($lists as $k => $v){
                    $total = 0;
                    if($mx){
                        foreach ($mx as $kk => $vv){
                            if($vv['shou_mchid'] == $v['mch_id']){
                                $total += $vv['shou_price'];
                            }
                        }
                    }
                    $lists[$k]['total_price'] = $total;
                }
            }

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }

        return $this->fetch();
    }

    /**
     * 添加子商户
     * @return mixed|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function mchAdd(){
        if(Request::instance()->isPost()){
            $mch_code = trim(input('mch_code'));

            $has = Db::table('up_mchid')->where('mch_code', $mch_code)->find();
            if($has) return json(['code' => 100, 'msg' => '该商户号已存在']);

            $insert = [
                'mch_code' => $mch_code,
                'mch_time' => date('Y-m-d H:i:s')
            ];
            $hasOne = Db::table('up_mchid')->where('mch_id', '>', 0)->find();
            if(!$hasOne) $insert['mch_default'] = 1;

            $res = Db::table('up_mchid')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /**
     * 设置为默认
     * @return \think\response\Json
     */
    public function setDefault(){
        $mch_id = input('id');

        Db::startTrans();
        try{
            Db::table('up_mchid')->where('mch_id', '<>', $mch_id)->update(['mch_default' => 2]);
            Db::table('up_mchid')->where('mch_id', $mch_id)->update(['mch_default' => 1]);

            Db::commit();
            return json(['code' => 200, 'msg' => '操作成功']);
        }catch (\Exception $e){
            Db::rollback();
            return json(['code' => 100, 'msg' => $e->getMessage()]);
        }
    }

    /**
     * 启用/禁用
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function setStatus(){
        $mch_id = input('id');
        $mch = Db::table('up_mchid')->where('mch_id', $mch_id)->field('mch_default, mch_status')->find();
        if($mch['mch_status'] == 1){
            if($mch['mch_default'] == 1) return json(['code' => 100, 'msg' => '默认收款商户号，不可以禁用']);
            $newStatus = 2;
        }else{
            $newStatus = 1;
        }
        $res = Db::table('up_mchid')->where('mch_id', $mch_id)->setField('mch_status', $newStatus);
        if($res){
            return json(['code' => 200, 'msg' => '操作成功']);
        }else{
            return json(['code' => 100, 'msg' => '操作失败']);
        }
    }
}